package cm.mefobe.android.telecomcost.dialog;

import java.util.ArrayList;
import cm.mefobe.android.telecomcost.R;
import cm.mefobe.android.telecomcost.DatabaseManager;
import cm.mefobe.android.telecomcost.TelecomCost;
import cm.mefobe.android.telecomcost.activities.WeeklyThresholdListActivity;
import cm.mefobe.android.telecomcost.adapter.WeeklyThresholdEditAdapter;
import cm.mefobe.android.telecomcost.entities.MonthlyThreshold;
import cm.mefobe.android.telecomcost.entities.WeeklyThreshold;
import cm.mefobe.android.telecomcost.view.WeeklyThresholdEditView;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class WeeklyThresholdDialog extends Dialog {

	private DatabaseManager db;
	
	private Button btnClose;
	private Button btnSave;
	
	private boolean shouldRefreshThresholdList = false;
	
	private Context _context;

	private ListView threshold_list;	

	/**
	 * @param context
	 * @param cancelable
	 * @param cancelListener
	 */
	public WeeklyThresholdDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		_context = context;
	}

	/**
	 * @param context
	 * @param theme
	 */
	public WeeklyThresholdDialog(Context context, int theme) {
		super(context, theme);
		_context = context;
	}

	public WeeklyThresholdDialog(Context context) {
		super(context);
		_context = context;
	}

	private void saveWeeklyThreshold() throws Exception {		
		Log.i("WeeklyThresholdDialog", "saving WeeklyThreshold...");		
		
		WeeklyThresholdListActivity activity = (WeeklyThresholdListActivity) _context;

		MonthlyThreshold currentMonth = activity.getMonthlyThreshold();
		ArrayList<WeeklyThreshold> thresholds = currentMonth.getWeeklyThresholds();
		WeeklyThresholdEditAdapter adapter = (WeeklyThresholdEditAdapter)threshold_list.getAdapter();
		int index = 0;
		
		while(index < thresholds.size())
		{
			WeeklyThreshold threshold = db.getWeekThreshold(thresholds.get(index).getWeek());
			WeeklyThresholdEditView view = (WeeklyThresholdEditView)adapter.getView(index, null, null);
			threshold.setAmount(view.getAmount());		
			db.saveWeeklyThreshold(threshold);
			
			index++;
		}
		
		shouldRefreshThresholdList = true;
		Log.i("WeeklyThresholdDialog", "weekly threshold saved...");
		Toast.makeText(_context, "Weekly threshold saved !!!", 3).show();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i("WeeklyThresholdDialog", "Creating the dialog...");
		super.onCreate(savedInstanceState);

		db = TelecomCost.getDatabaseManager();
		setContentView(R.layout.day_range_threshold_dialog);

		setTitle("Weekly Threshold");

		threshold_list = (ListView) findViewById(R.id.listViewThreshold);
		
		btnClose = (Button) findViewById(R.id.btnClose);
		btnSave = (Button) findViewById(R.id.btnSave);

		btnClose
				.setOnClickListener(new View.OnClickListener() {

					public void onClick(View arg0) {
						closeDialog();
					}
				});

		btnSave.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				try {
					saveWeeklyThreshold();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		Log.i("WeeklyThresholdDialog", "Dialog ohne bundle created....");
	}

	protected void closeDialog() {
		if (shouldRefreshThresholdList) {
			((WeeklyThresholdListActivity) _context).bindData();
		}
		
		hide();
	}

	@Override
	public void show() {
		Log.i("WeeklyThresholdDialog", "showing the dialog...");
		super.show();
		Log.i("WeeklyThresholdDialog", "dialog on screen...");
		bindWeeklyTreshold();
	}

	@Override
	protected void onStart() {
		Log.i("WeeklyThresholdDialog", "Starting the dialog...");
		super.onStart();
		Log.i("WeeklyThresholdDialog", "dialog started...");
	}

	private void bindWeeklyTreshold() {
		Log.i("WeeklyThresholdDialog", "binding WeeklyThresholdDialog...");

		WeeklyThresholdListActivity activity = (WeeklyThresholdListActivity) _context;
		MonthlyThreshold currentMonth = activity.getMonthlyThreshold();

		if(threshold_list != null)
		{
			if (currentMonth != null) {
				Log.i("WeeklyThresholdListActivity", "binding WeeklyThreshold...");
				ArrayList<WeeklyThreshold> thresholds = currentMonth.getWeeklyThresholds();

				threshold_list.setAdapter(new WeeklyThresholdEditAdapter(
						_context, R.layout.threshold_edit_view,
						thresholds));
			} else {
				Log.i("WeeklyThresholdListActivity",
						"cannot bind the weekly threshold...set the month and week first");
			}
		}	
		else {
			Log.i("WeeklyThresholdListActivity",
					"cannot bind the weekly threshold...set the threshold_list first");
		}
	}

	@Override
	protected void onStop() {
		Log.i("WeeklyThresholdDialog", "Stopping the dialog...");
		super.onStop();
		Log.i("WeeklyThresholdDialog", "dialog stopped...");
	}
}
